package tcpfiledemo2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-08-09
 * Time: 17:40
 */
//读取到客户端发来的文件，然后保存到本地，最后发送给客户端说文件上传成功
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket  serverSocket=new ServerSocket(1000);
    
        //别忘记了建立了建立来链接
        Socket accept=serverSocket.accept();
        //网络中的流,从客户端读取数据的
        BufferedInputStream bis=new BufferedInputStream(accept.getInputStream());
        //本地的流，写到本地，永久存储
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("src/ServerImg/copy.jpg"));
        //从网络流中读取到本地
        int b;
        while ((b=bis.read())!=-1){
            //写到本地的输出流
            bos.write(b);
        }
        
        //向客户端发送“上传成功”
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        bw.write("上传成功");
        bw.newLine();
        bw.flush();
        //关闭本地链接
        bos.close();
        accept.close();
        serverSocket.close();
    }
}
